﻿using MyUAMS.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyInformation_MyBRAP : System.Web.UI.Page
{
    int uid;
    int apid;
    int rid;
    WorkServer assetSrv = new WorkServer();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
        }
    }
    protected void Bind()
    {
        if (Session["CustomerId"] != null)
        {
            int uid = int.Parse(Session["CustomerId"].ToString());
            var products = assetSrv.GetBRByUid(uid);
            if (products.Count() != 0)
            {
                dgvInformation.DataSource = products;
                dgvInformation.DataBind();
            }
            else
            {
                lblProErr.Text = " ";
            }
            
        }
        
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        GridView gvProduct = new GridView();
        gvProduct = (GridView)Page.FindControl("dgvInformation");
        if (gvProduct != null)
        {
            for (int i = 0; i < gvProduct.Rows.Count; i++)
            {
                CheckBox chkChoice = new CheckBox();
                chkChoice = (CheckBox)gvProduct.Rows[i].FindControl("chkChoice");
                if (chkChoice != null)
                {
                    if (chkChoice.Checked)
                    {
                        if (gvProduct.Rows[i].Cells[13].Text!="审核中" && gvProduct.Rows[i].Cells[13].Text != "打回")
                        {
                            
                            apid = int.Parse(gvProduct.Rows[i].Cells[2].Text);
                            rid = int.Parse(gvProduct.Rows[i].Cells[1].Text);
                            assetSrv.UpdateAPInBRByUid(apid,rid, int.Parse(txtRamount.Text));
                            Bind();


                        }
                        else
                        {
                            lblProErr.Text = "请等待审核";
                        }
                        
                    }
                }
                else
                {
                    lblProErr.Text = "请选择要还入的资产";
                }
            }
        }
    }

    protected void return_Click(object sender, ImageClickEventArgs e)
    {
        // 获取触发事件的 ImageButton
        ImageButton returnButton = sender as ImageButton;

        // 确保返回按钮不为 null
        if (returnButton != null)
        {
            // 获取包含 ImageButton 的 GridViewRow
            GridViewRow row = returnButton.NamingContainer as GridViewRow;

            // 确保行不为 null
            if (row != null)
            {
                // 从 DataKeys 集合中获取 APid
                int aid = Convert.ToInt32(dgvInformation.DataKeys[row.RowIndex].Value);
                lblProErr.Text = aid.ToString();
                // 构建跳转 URL 并传递参数 APid
                string redirectUrl = "../UserWork/BRForm.aspx?Apid=" + aid;

                // 重定向到目标页面
                Response.Redirect(redirectUrl);
            }
        }
    }
}